package com.jftone.bh.app.directive;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.data.StaticInfoQry;
import com.jftone.bh.app.model.SysMenuFunc;
import com.jftone.bh.app.service.SysMenuService;
import com.jftone.bh.app.service.SysRoleService;
import org.apache.log4j.Logger;
import org.jftone.component.BeanContext;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;


import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateModel;

public class FuncOperDirective implements TemplateDirectiveModel {
	
	private Logger logger = Logger.getLogger(FuncOperDirective.class);
			
	private String roleCode;
	private String rightCode;
	public FuncOperDirective(String roleCode, String rightCode){
		super();
		this.roleCode = roleCode;
		this.rightCode = rightCode;
	}
	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body)  {
		try {
			Writer out = env.getOut(); 
            out.write(getFuncInfo());  
            if(body != null){
            	 body.render(out);  
            }
            out.flush();
		} catch (Exception e) {
			logger.error("自定义菜单功能指令错误", e);
		}
	}
	
	/**
	 * 功能按钮解析后数据
	 * @return
	 */
	private String getFuncInfo(){
		if(StringUtil.isBlank(rightCode) || StringUtil.isBlank(roleCode)){
			return "";
		}
		StringBuilder sb = new StringBuilder();
		try {
			List<IData<String, Object>> list = new ArrayList<IData<String, Object>>();
			for(String code : roleCode.split(",")){
				IData<String, Object> inData = new DataMap<String, Object>();
				inData.put("ROLE_CODE", code);
				inData.put("MENU_CODE", rightCode);
				list.add(inData);
			}
			
			SysRoleService sysRoleService = BeanContext.getBean(SysRoleService.class);
			List<IData<String, Object>> srList = sysRoleService.getRightByRoleCode(list);
			if(null == srList || srList.size()==0) return "";
			StringBuilder funcSb = new StringBuilder();
			for(IData<String, Object> tmp : srList){
				funcSb.append((funcSb.length()<=0 ? "" : ",")+ tmp.get("FUNC_SET"));
			}
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("menuCode", rightCode);
			SysMenuService sysMenuService = BeanContext.getBean(SysMenuService.class);
			List<SysMenuFunc> ml = sysMenuService.querySysMenuFunc(inData);
			
			sb.append("<div class=\"l_mt\">");
			Map<String, String> data = StaticInfoQry.getParamMap(SysConst.MENU_FUNC_BTN);
			for(SysMenuFunc mf : ml){
				if(funcSb.indexOf(mf.getFuncCode()) == -1) continue;
				sb.append("<button id=\""+mf.getFuncCode()+"\" name=\""+mf.getFuncCode()+"\" tag=\""+mf.getFuncType()+"\" limit=\""+mf.getLimitNum()+"\" cmd=\""+mf.getParam()+"\" type=\"button\" bind=\"oper\" class=\"e_btn\">"+data.get(mf.getFuncCode())+"</button>&nbsp;&nbsp;");
			}
			sb.append("</div>");

		} catch (Exception e) {
			logger.error("读取菜单功能数据错误", e);
		}
		return sb.toString();
	}

}
